library(UsingR)

data("babies")

head(babies)

#s para checar as diferenas com a base babies do UsingR

#Criado para o 7.2.a.
babies <- read.csv("Planilha de dados Babies.txt", sep = " ")

head(babies)

# Ajustes

babies <- babies[which(babies$bwt!=999,),]
babies <- babies[which(babies$gestation!=999,),]
babies <- babies[which(babies$parity!=9,),]
babies <- babies[which(babies$age!=99,),]
babies <- babies[which(babies$height!=99,),]
babies <- babies[which(babies$weight!=999,),]
babies <- babies[which(babies$smoke!=9,),]
babies$bwtadj <-babies$bwt*0.0283 

head(babies)

# Peso em funao da duraao da gestaao
str(babies)
modelo <- lm(babies$bwtadj ~ babies$gestation)
confint(modelo)
summary(modelo)
anova(modelo)

# Sequencia de valores de durao da gestao
summary(modelo)
seqpred <- round(seq(from = 150 , to = 350, length.out = 100))
seqpred

# Erro padro
s2 <- var(babies$bwtadj)
ssx <- sum((babies$gestation - mean(babies$gestation))^2)
n <- 1174
erropad <- sqrt((s2*((1/n)+((seqpred - mean(babies$gestation))^2))/ssx))
plot(erropad)

#limites do intervalo de confiana
tvalor <- qt(p = 0.95, df = 1172)
interconf <- erropad * tvalor
pesopred <- coef(modelo)[1] + coef(modelo)[2]*seqpred
liminf <- pesopred - interconf
limsup <- interconf + pesopred 

plot(babies$bwtadj ~ babies$gestation, ylab = "Massa ao nascer (kg)", xlab = "Durao da gestao (dias)", pch = 20, cex = 0.8)
abline(a = coef(modelo)[1], b = coef(modelo)[2])
lines(limsup ~ seqpred)
lines(liminf ~ seqpred)
